package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.QueryCycleTypeInstanceDetailReq;
import com.sg.dto.base.app.req.QueryCycleTypeInstanceListReq;
import com.sg.dto.base.app.req.QueryTenantCycleTypeInstanceListReq;
import com.sg.entity.OmsCycleTypeInstance;
import com.sg.service.base.app.MOmsCycleTypeInstanceService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsCycleTypeInstanceMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsCycleTypeInstanceServiceImpl
        extends ServiceImpl<OmsCycleTypeInstanceMapper, OmsCycleTypeInstance>
        implements MOmsCycleTypeInstanceService {

    @Autowired
    OmsCycleTypeInstanceMapper omsCycleTypeInstanceMapper;

    /**
     * 3-4-03查周期类型实例详情[1894]
     * gen by moon at 10/13/2022, 10:47:23 PM
     */
    @Trace(operationName = "3-4-03查周期类型实例详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsCycleTypeInstance queryCycleTypeInstanceDetail(QueryCycleTypeInstanceDetailReq reqDto) {

        try {

            QueryWrapper<OmsCycleTypeInstance> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsCycleTypeInstance> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsCycleTypeInstance retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-4-02查周期类型实例列表[1863]
     * gen by moon at 10/13/2022, 10:47:22 PM
     */
    @Trace(operationName = "3-4-02查周期类型实例列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsCycleTypeInstance> queryCycleTypeInstanceList(QueryCycleTypeInstanceListReq reqDto) {

        try {
            QueryWrapper<OmsCycleTypeInstance> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsCycleTypeInstance.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsCycleTypeInstance.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsCycleTypeInstance.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsCycleTypeInstance>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-4-02修改周期类型实例[1862]
     * gen by moon at 10/13/2022, 10:47:21 PM
     */
    @Trace(operationName = "3-4-02修改周期类型实例")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCycleTypeInstance(OmsCycleTypeInstance omsCycleTypeInstance) {
        omsCycleTypeInstance.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsCycleTypeInstance != null && omsCycleTypeInstance.getSubjectLifeCycle() != null && omsCycleTypeInstance.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleTypeInstance.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsCycleTypeInstance.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsCycleTypeInstance != null && omsCycleTypeInstance.getSubjectLifeCycle() != null && omsCycleTypeInstance.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleTypeInstance.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        return super.updateById(omsCycleTypeInstance);
    }

    /**
     * 3-4-02新增周期类型实例[1861]
     * gen by moon at 10/13/2022, 10:47:20 PM
     */
    @Trace(operationName = "3-4-02新增周期类型实例")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCycleTypeInstance(OmsCycleTypeInstance omsCycleTypeInstance) {
        omsCycleTypeInstance.setCycleTypeInstanceId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsCycleTypeInstance.setIsArchive("FALSE");
        omsCycleTypeInstance.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsCycleTypeInstance.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsCycleTypeInstance != null && omsCycleTypeInstance.getSubjectLifeCycle() != null && omsCycleTypeInstance.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleTypeInstance.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsCycleTypeInstance.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsCycleTypeInstance.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsCycleTypeInstance != null && omsCycleTypeInstance.getSubjectLifeCycle() != null && omsCycleTypeInstance.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsCycleTypeInstance.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsCycleTypeInstance.setIsValid("FALSE");
        omsCycleTypeInstance.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsCycleTypeInstance.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsCycleTypeInstance)) {
            return omsCycleTypeInstance.getCycleTypeInstanceId();
        } else {
            return "-1";
        }

    }

    /**
     * 删除周期类型实例[1864]
     * gen by moon at 10/13/2022, 10:47:22 PM
     */
    @Trace(operationName = "删除周期类型实例")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCycleTypeInstance(String cycleTypeInstanceId) {

        return super.removeById(cycleTypeInstanceId);
    }

    /**
     * 3-4-02查询租户周期类型实例列表[5324]
     * gen by moon at 11/29/2022, 7:35:33 PM
     */
    @Trace(operationName = "3-4-02查询租户周期类型实例列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsCycleTypeInstance> queryTenantCycleTypeInstanceList(QueryTenantCycleTypeInstanceListReq reqDto) {

        try {
            QueryWrapper<OmsCycleTypeInstance> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsCycleTypeInstance.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsCycleTypeInstance.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsCycleTypeInstance.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsCycleTypeInstance>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
